﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using VietImage.Models;
using VietImage.Logs;

namespace VietImage.Roles
{
    public class TranslatorRole : RegisteredUserRole
    {
        #region Methods
        public List<ErrorItem> Translate(System.Object ob, int objectKind, int id_Language)
        {
            LogTranslation log = new LogTranslation();
            log.ID_User = this.UserInfo.ID;
            log.ID_Action = 6;
            log.ID_Language = id_Language;
            log.ObjectKind = objectKind;

            List<ErrorItem> errorList = new List<ErrorItem>();

            VietImage.Models.VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            switch (objectKind)
            {
                //Image
                case 1:
                    {
                        DetailImage i = DetailImage.Parse(ob);
                        errorList = DetailImage.ChangeInformation(i, this.UserInfo.ID, this.ID_Language);

                        LogImage li = _db.LogImages.Where(p => p.ID_Image == i.ID_Image && p.ID_User == this.UserInfo.ID).OrderByDescending(p => p.ID).First();
                        log.ID_ChangeLog = li.ID;

                        if (errorList != null)
                            VietImageDB_Log.WriteLog("Image cannot be translated.");

                        break;
                    }
                //Category
                case 2:
                    {
                        DetailCategory i = DetailCategory.Parse(ob);
                        errorList = DetailCategory.ChangeInformation(i, this.UserInfo.ID, this.ID_Language);

                        LogCategory li = _db.LogCategories.Where(p => p.ID_Category == i.ID_Category && p.ID_User == this.UserInfo.ID).OrderByDescending(p => p.ID).First();
                        log.ID_ChangeLog = li.ID;

                        if (errorList != null)
                            VietImageDB_Log.WriteLog("Category cannot be translated.");

                        break;
                    }
                //Customer
                case 3:
                    {
                        DetailCustomer i = DetailCustomer.Parse(ob);
                        errorList = DetailCustomer.ChangeInformation(i, this.UserInfo.ID, this.ID_Language);

                        LogCustomer li = _db.LogCustomers.Where(p => p.ID_Customer == i.ID_Customer && p.ID_User == this.UserInfo.ID).OrderByDescending(p => p.ID).First();
                        log.ID_ChangeLog = li.ID;

                        if (errorList != null)
                            VietImageDB_Log.WriteLog("Customer cannot be translated.");

                        break;
                    }
                //Info
                case 4:
                    {
                        DetailInfo i = DetailInfo.Parse(ob);
                        errorList = DetailInfo.ChangeInformation(i, this.UserInfo.ID, this.ID_Language);

                        LogInfo li = _db.LogInfos.Where(p => p.ID_Info == i.ID_Info && p.ID_User == this.UserInfo.ID).OrderByDescending(p => p.ID).First();
                        log.ID_ChangeLog = li.ID;

                        if (errorList != null)
                            VietImageDB_Log.WriteLog("Info cannot be translated.");

                        break;
                    }
                //Email
                case 5:
                    {
                        Email i = Email.Parse(ob);
                        errorList = Email.ChangeInformation(i, this.UserInfo.ID, this.ID_Language);

                        LogEmail li = _db.LogEmails.Where(p => p.ID_Email == i.ID_Email && p.ID_User == this.UserInfo.ID).OrderByDescending(p => p.ID).First();
                        log.ID_ChangeLog = li.ID;

                        if (errorList != null)
                            VietImageDB_Log.WriteLog("Customer cannot be translated.");
                        
                        break;
                    }
                //User
                case 6:
                    {
                        DetailUser i = DetailUser.Parse(ob);
                        errorList = DetailUser.ChangeInformation(i, this.UserInfo.ID, this.ID_Language);

                        LogUser li = _db.LogUsers.Where(p => p.ID_PassiveUser == i.ID_User && p.ID_ActiveUser == this.UserInfo.ID).OrderByDescending(p => p.ID).First();
                        log.ID_ChangeLog = li.ID;

                        if (errorList != null)
                            VietImageDB_Log.WriteLog("User cannot be translated.");

                        break;
                    }
            }

            if (errorList == null)
            {
                errorList = LogTranslation.Insert(this.UserInfo.ID, log.ID_Language, log.ID_Object, log.ObjectKind, log.ID_ChangeLog.Value, this.ID_Language);
                if (errorList != null)
                {
                    VietImageDB_Log.WriteLog("Cannot write translations log.");
                    VietImageDB_Log.WriteLog("Error list : ");
                    foreach (ErrorItem kvp in errorList)
                        VietImageDB_Log.WriteLog("---" + kvp.Key + " error : " + kvp.Message);
                }
                else
                    VietImageDB_Log.WriteLog(this.UserInfo.Username + " translated object No." + log.ID_Object + " - Kind : " + log.ObjectKind + " .");
            }
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot translated object No." + log.ID_Object + " - Kind : " + log.ObjectKind + " .");

            return errorList;
        }

        public System.Object ViewTranslationList(int objectKind, bool nonTranslated)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            System.Object list = null;

            if (nonTranslated)
                list = LogTranslation.GetNonTranslatedObjectList(objectKind);
            else
                list = LogTranslation.GetTranslatedObjectList(objectKind);

            return list;
        }
        #endregion

        #region Reports
        public System.Object TranslationReport(int objectKind, DateTime date, bool isTranslated)
        {
            System.Object list = null;
            if (isTranslated)
                list = LogTranslation.GetTranslatedObjectList(objectKind, date);
            else
                list = LogTranslation.GetNonTranslatedObjectList(objectKind, date);

            return list;
        }

        public System.Object TranslationReport(int objectKind, DateTime beginDate, DateTime endDate, bool isTranslated)
        {
            System.Object list = null;
            if (isTranslated)
                list = LogTranslation.GetTranslatedObjectList(objectKind, beginDate, endDate);
            else
                list = LogTranslation.GetNonTranslatedObjectList(objectKind, beginDate, endDate);

            return list;
        }

        public System.Object TranslationReport(int objectKind, int month, int year, bool isTranslated)
        {
            System.Object list = null;
            if (isTranslated)
                list = LogTranslation.GetTranslatedObjectList(objectKind, month, year);
            else
                list = LogTranslation.GetNonTranslatedObjectList(objectKind, month, year);

            return list;
        }
        #endregion
    }
}